﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;

namespace MyVocationSpace
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            BAL.conn.connectionString = ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString;
            if (Session["UserID"] != null)
            {

            }
            if (Session["UserName"] != null)
            {
                //lnkLogin.Text = Session["UserName"].ToString();
                
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string strEmail = txtEmail.Text;
            string strPassword = BAL.Encryption.Encript(txtPassword.Text, "&%#@?,:*");
            string UserID = BAL.Users.VerifyUserID(strEmail, strPassword);

            if (UserID == "-1" || UserID == "-2")
            {
                idSpanMsg.Style.Add("display", "block");
                idSpanMsg.InnerHtml = "Sorry!! Email ID or Password you entered is incorrect";
                //ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "ErrorPopup", "ErrorPopup('Sorry!! Email ID or Password you entered is incorrect');", true);

            }
            else if (UserID == "0")
            {
                idSpanMsg.Style.Add("display", "block");
                idSpanMsg.InnerHtml = "Your Email ID is not verified yet. Click on Confirmation link sent to your Email ID to verify.";
                //ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "ErrorPopup", "ErrorPopup('Your Email ID is not verified yet. <br> Click on Confirmation link sent to your Email ID to verify.');", true);
                // "Your Email ID is not verified yet. <br> Click on Confirmation link sent to your Email ID to verify.";
            }
            else
            {
                //Session.Add("UserID", strVal);

                Session.Add("UserID", UserID);
                Session.Add("UserEmail", strEmail);
                string[] userDetails = BAL.Users.GetUserDetails(Convert.ToInt32(UserID));
                string Name = userDetails[1] + " " + userDetails[2];
                //lnkLogin.Text = Name;

                Session.Add("UserName", Name);

                //string isAdmin = userDetails[3];
                //Session.Add("IsAdmin", isAdmin);

                string Country = userDetails[15];
                Session.Add("IDCountry", Country);

                string IDIndustry = userDetails[17];
                Session.Add("IDIndustry", IDIndustry);

                bool admiflag = BAL.Users.GetAdminFlag(Convert.ToInt32(UserID));
                Session.Add("IsAdmin", admiflag);

                DataSet ds = BAL.Users.GetVocationByUser(Convert.ToInt32(Session["UserID"]));
                if (ds.Tables.Count > 0)
                {
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        Response.Redirect("MyDashboard.aspx");
                    }
                    else
                    {
                        Response.Redirect("AddMyVocation.aspx");
                    }
                }
            }
        }
    }
}